package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Classes;
import com.liushuai.eas.pojo.Course;
import com.liushuai.eas.pojo.Major;
import com.liushuai.eas.pojo.Student;
import com.liushuai.eas.service.ClassesImp;
import com.liushuai.eas.service.MajorImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classes")
public class ClassesController {

    @Resource
    private ClassesImp classesImp;

    @Resource
    private MajorImp majorImp;

    @PostMapping
    public Result<?> add(@RequestBody Classes classes){

        //插入classes数据库
        if (classesImp.insert(classes)!=1)
            return Result.fail(classes);

        return Result.success(classes);
    }

    @PutMapping
    public Result<Classes> update(@RequestBody Classes classes){
        if (classesImp.updateById(classes)!=1)
            return Result.fail(classes);
        return Result.success(classes);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        List<Classes> classList;
        int total = 0;
        HashMap<String,Object> map = new HashMap<String,Object>();

        if (search.isEmpty()){
            classList = classesImp.queryClassesList((pageNum - 1) * pageSize,  pageSize);
            total = classesImp.queryTotal();
        }else {
            classList = classesImp.queryClassesListBySearch((pageNum - 1) * pageSize, pageSize,search);
            total = classesImp.queryTotalBySearch(search);
        }

        for(Classes classes : classList){
            classes.setMajor(majorImp.selectOne(classes.getMajorId()));
        }

        map.put("records",classList);
        map.put("total",total);
        map.put("pageSize",pageSize);
        return Result.success("数据加载成功",map);
    }

    @DeleteMapping
    public Result<Classes> delete(@RequestBody Classes classes){
        if (classesImp.deleteById(classes)!=1)
            return Result.fail(classes);
        return Result.success(classes);
    }

    @GetMapping("/selectAll")
    public Result<List<Classes>> selectAll(){
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        List<Classes> classesList = classesImp.selectList(queryWrapper);

        return Result.success(classesList);
    }

    @PostMapping("/queryClassById")
    public Result<Classes> queryClassById(@RequestBody Map<String,Integer> map){

        Integer classId = map.get("classId");

        Classes classes = classesImp.selectOne(classId);

        return Result.success(classes);
    }

}
